import sys #system functions (write, etc)
import urllib #url functions (download, encode, etc)
import zipfile #zip compression decompression
import bz2 #bzip compression decompression
import re #regular expressions
import Image #image management
import cPickle #serialization
import xmlrpclib #xmlrpc web service management
import time #get time info
import pdb #debug using pdb.set_trace()

def DownloadFile(url,path):
	"""Downloads a file from an url to a file system path.
url: file url to download
path: filesystem path to save the downloaded file"""
	
	fileData = DownloadFileS(url)
	SaveFile(fileData,path)
	
def DownloadFileS(url):
	"""Downloads a file from an url to a file system path.
url: file url to download
path: filesystem path to save the downloaded file"""
	
	fileData = urllib.urlopen(url).read()
	return fileData

def SaveFile(data,path):
	out = open(path,"wb")
	out.write(data)
	out.flush()
	out.close()

	
def Serialize(data,path,binary = False):
	out = open(path,"wb")
	cPickle.dump(data,out,binary)
	out.flush()
	out.close()
	
def Deserialize(path):
	input = open(path,"r")
	data = cPickle.load(input)
	input.close()
	return data

logEnabled = False
logSeparator = "--------------------"

def Log(message = logSeparator, error = None):
	if not logEnabled:
		return None
		
	nowTime = time.localtime()
	
	out = open("log.txt","a+")
	out.write("[%s]: " % (Now())+message+"\n")
	if error != None:
		out.write("[%s]: " % (Now()) +error+"\n")
	out.flush()
	out.close()

def Now():
	return time.strftime("%Y-%m-%d %H:%M:%S")
